package com.lancer.cloud.common.config;

import com.google.common.collect.Lists;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger2 配置
 *
 * @author caijinbang
 * @date 2019-06-15 22:49
 */
@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    /**
     * 创建API应用，ApiInfo()增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例，用来控制那些接口暴露给Swagger来展现
     * @return
     */
    @Bean
    public Docket createRestApi() {

        ParameterBuilder token = new ParameterBuilder();
        Parameter build = token.name("auth").defaultValue("token").description("token令牌")
            .modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        List<Parameter> parameterList = Lists.newArrayList();
        return  new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo()).globalOperationParameters(parameterList)
            .select()
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            .paths(PathSelectors.any())
            .build();
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("Lancer Service API")
            .description("Lancer Service 接口文档说明")
            .contact(new Contact("jinbangcai", "", "506169738@qq.com"))
            .version("1.0")
            .build();
    }
}
